package com.minglead.springboot.platform.sys.entity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import com.minglead.springboot.common.config.Global;
import com.minglead.springboot.common.persistence.DataEntity;
import com.minglead.springboot.platform.sys.bean.CommonBean;

/**
 * 角色Entity
 */
public class Role extends DataEntity<Role> {
	
	private static final long serialVersionUID = 1L;
	private Office office;	// 归属机构
	private String name; 	// 角色名称
	private String code;	// 英文code
	private String roleType;// 权限类型
	private String dataScope;// 数据范围
	
	private String oldName; 	// 原角色名称
	private String oldCode;	// 原英文名称
	private String sysData; 		//是否是系统数据
	private String useable; 		//是否是可用
	
	private User user;		// 根据用户ID查询角色列表

//	private List<User> userList = Lists.newArrayList(); // 拥有用户列表
	private List<Menu> menuList = new ArrayList<Menu>(); // 拥有菜单列表
	private List<Office> officeList = new ArrayList<Office>(); // 按明细设置数据范围

	private String dataScopeValue;// 数据范围（显示）
	
	// 数据范围（1：所有数据；5：所在部门数据；4：所在部门及以下数据；8：仅本人数据；9：按明细设置）
	public enum DataScopeEnum{
		data_scope_all,data_scope_office,data_scope_office_and_child,data_scope_self,data_scope_custom
	}

	/**
	 * 	// 数据范围（1：所有数据；4：所在部门及以下数据；5：所在部门数据；8：仅本人数据；9：按明细设置）
	 * @return
	 */
	public static List<CommonBean> getDataScopeList(){	
		
		List<CommonBean> list = new ArrayList<CommonBean>();		
		CommonBean c_1 = new CommonBean();
		c_1.setValue(DataScopeEnum.data_scope_all.name().toString());
		c_1.setLabel(Role.getDataScopeLabel(c_1.getValue()));	
	
	
		CommonBean c_5 = new CommonBean();
		c_5.setValue(DataScopeEnum.data_scope_office.name().toString());
		c_5.setLabel(Role.getDataScopeLabel(c_5.getValue()));	
		
		CommonBean c_4 = new CommonBean();
		c_4.setValue(DataScopeEnum.data_scope_office_and_child.name().toString());
		c_4.setLabel(Role.getDataScopeLabel(c_4.getValue()));	
		

		CommonBean c_8 = new CommonBean();
		c_8.setValue(DataScopeEnum.data_scope_self.name().toString());
		c_8.setLabel(Role.getDataScopeLabel(c_8.getValue()));	
		
		
		CommonBean c_9 = new CommonBean();
		c_9.setValue(DataScopeEnum.data_scope_custom.name().toString());
		c_9.setLabel(Role.getDataScopeLabel(c_9.getValue()));	
			
		list.add(c_1);
		list.add(c_5);
		list.add(c_4);
		list.add(c_8);
		list.add(c_9);
		return list;
	}

	public static String getDataScopeLabel(String type){
		if(StringUtils.isEmpty(type))
			return "null";
		if(DataScopeEnum.data_scope_all.name().toString().equals(type)){
			return "所有数据";
		}else if(DataScopeEnum.data_scope_office.name().toString().equals(type)){
			return "所在部门数据";
		}else if(DataScopeEnum.data_scope_office_and_child.name().toString().equals(type)){
			return "所在部门及以下数据";
		}else if(DataScopeEnum.data_scope_self.name().toString().equals(type)){
			return "仅本人数据";
		}else if(DataScopeEnum.data_scope_custom.name().toString().equals(type)){
			return "按明细设置";
		}else{
			return "未知状态";
		}
	}

	
	public Role() {
		super();
		this.dataScope = "1";//默认所有
		this.useable=Global.YES;
	}
	
	public Role(String id){
		super(id);
	}
	
	public Role(User user) {
		this();
		this.user = user;
	}

	public String getUseable() {
		return useable;
	}

	public void setUseable(String useable) {
		this.useable = useable;
	}

	public String getSysData() {
		return sysData;
	}

	public void setSysData(String sysData) {
		this.sysData = sysData;
	}

	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}

	@Length(min=1, max=100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Length(min=1, max=100)
	public String getRoleType() {
		return roleType;
	}
	@Length(min=0, max=64)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public String getDataScope() {
		return dataScope;
	}

	public void setDataScope(String dataScope) {
		this.dataScope = dataScope;
	}

	public String getOldName() {
		return oldName;
	}

	public void setOldName(String oldName) {
		this.oldName = oldName;
	}



//	public List<User> getUserList() {
//		return userList;
//	}
//
//	public void setUserList(List<User> userList) {
//		this.userList = userList;
//	}
//	
//	public List<String> getUserIdList() {
//		List<String> nameIdList = Lists.newArrayList();
//		for (User user : userList) {
//			nameIdList.add(user.getId());
//		}
//		return nameIdList;
//	}
//
//	public String getUserIds() {
//		return StringUtils.join(getUserIdList(), ",");
//	}

	public String getOldCode() {
		return oldCode;
	}

	public void setOldCode(String oldCode) {
		this.oldCode = oldCode;
	}

	public List<Menu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}

	public List<String> getMenuIdList() {
		List<String> menuIdList = new ArrayList<String>();
		for (Menu menu : menuList) {
			menuIdList.add(menu.getId());
		}
		return menuIdList;
	}

	public void setMenuIdList(List<String> menuIdList) {
		menuList = new ArrayList<Menu>();
		for (String menuId : menuIdList) {
			Menu menu = new Menu();
			menu.setId(menuId);
			menuList.add(menu);
		}
	}

	public String getMenuIds() {
		return StringUtils.join(getMenuIdList(), ",");
	}
	
	public void setMenuIds(String menuIds) {
		menuList = new ArrayList<Menu>();
		if (menuIds != null){
			String[] ids = StringUtils.split(menuIds, ",");
			List<String> list = new ArrayList<String>();
			Collections.addAll(list, ids);
			setMenuIdList(list);
		}
	}
	
	public List<Office> getOfficeList() {
		return officeList;
	}

	public void setOfficeList(List<Office> officeList) {
		this.officeList = officeList;
	}

	public List<String> getOfficeIdList() {
		List<String> officeIdList = new ArrayList<String>();
		for (Office office : officeList) {
			officeIdList.add(office.getId());
		}
		return officeIdList;
	}

	public void setOfficeIdList(List<String> officeIdList) {
		officeList = new ArrayList<Office>();
		for (String officeId : officeIdList) {
			Office office = new Office();
			office.setId(officeId);
			officeList.add(office);
		}
	}

	public String getOfficeIds() {
		return StringUtils.join(getOfficeIdList(), ",");
	}
	
	public void setOfficeIds(String officeIds) {
		officeList = new ArrayList<Office>();
		if (officeIds != null){
			String[] ids = StringUtils.split(officeIds, ",");		
			List<String> list = new ArrayList<String>();
			Collections.addAll(list, ids);	
			setOfficeIdList(list);
		}
	}
	
	/**
	 * 获取权限字符串列表
	 */
	public List<String> getPermissions() {
		List<String> permissions = new ArrayList<String>();
		for (Menu menu : menuList) {
			if (menu.getPermission()!=null && !"".equals(menu.getPermission())){
				permissions.add(menu.getPermission());
			}
		}
		return permissions;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getDataScopeValue() {
		return dataScopeValue;
	}

	public void setDataScopeValue(String dataScopeValue) {
		this.dataScopeValue = dataScopeValue;
	}
	
	

//	public boolean isAdmin(){
//		return isAdmin(this.id);
//	}
//	
//	public static boolean isAdmin(String id){
//		return id != null && "1".equals(id);
//	}
	
//	@Transient
//	public String getMenuNames() {
//		List<String> menuNameList = Lists.newArrayList();
//		for (Menu menu : menuList) {
//			menuNameList.add(menu.getName());
//		}
//		return StringUtils.join(menuNameList, ",");
//	}
}
